﻿@using SimpleIdServer.IdServer.Api.AuthenticationSchemeProviders;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.IdProviderStore;
@inject IState<UpdateIdProviderState> updateIdProviderState

<RadzenTemplateForm Submit=@SubmitUpdateUserAttributeIdProvider TItem="UserAttributeIdProviderMapperForm" Data=@userAttributeIdProviderMapperForm>
    <!-- Name -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Name</RadzenText>
        <RadzenTextBox Name="Name" @bind-Value="@userAttributeIdProviderMapperForm.Name" Class="w-100"></RadzenTextBox>
        <RadzenRequiredValidator Component="Name" Text="@Global.NameIsRequired"></RadzenRequiredValidator>
    </div>
    <!-- Token Claim Name -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.SourceClaimName</RadzenText>
        <RadzenTextBox Name="SourceClaimName" @bind-Value="@userAttributeIdProviderMapperForm.SourceClaimName" Class="w-100"></RadzenTextBox>
        <RadzenRequiredValidator Component="SourceClaimName" Text="@Global.SourceClaimNameRequired"></RadzenRequiredValidator>
    </div>
    <!-- User attribute -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.UserAttribute</RadzenText>
        <RadzenTextBox Name="TargetUserAttribute" @bind-Value="@userAttributeIdProviderMapperForm.TargetUserAttribute" Class="w-100"></RadzenTextBox>
        <RadzenRequiredValidator Component="TargetUserAttribute" Text="@Global.UserAttributeRequired"></RadzenRequiredValidator>
    </div>
    @if(IsEnabled)
    {        
        <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@(updateIdProviderState.Value.IsUpdating ? Global.Saving : Global.Save)" Disabled="@(updateIdProviderState.Value.IsUpdating)" />
    }
</RadzenTemplateForm>

@code {
    [Parameter]
    public EventCallback<AuthenticationSchemeProviderMapperResult> AuthProviderMapperUpdated { get; set; }
    [Parameter]
    public AuthenticationSchemeProviderMapperResult Mapper { get; set; }
    [Parameter]
    public bool IsEnabled { get; set; } = true;

    record UserAttributeIdProviderMapperForm
    {
        public string Name { get; set; }
        public string SourceClaimName { get; set; }
        public string? TargetUserAttribute { get; set; } = null;
    }

    UserAttributeIdProviderMapperForm userAttributeIdProviderMapperForm = new UserAttributeIdProviderMapperForm();

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if(Mapper != null)
        {
            userAttributeIdProviderMapperForm.Name = Mapper.Name;
            userAttributeIdProviderMapperForm.SourceClaimName = Mapper.SourceClaimName;
            userAttributeIdProviderMapperForm.TargetUserAttribute = Mapper.TargetUserAttribute;
        }
    }

    async void SubmitUpdateUserAttributeIdProvider(UserAttributeIdProviderMapperForm value)
    {
        await AuthProviderMapperUpdated.InvokeAsync(new AuthenticationSchemeProviderMapperResult
        {
            Name = value.Name,
            SourceClaimName = value.SourceClaimName,
            TargetUserAttribute = value.TargetUserAttribute,
            MapperType = MappingRuleTypes.USERATTRIBUTE
        });
    }
}